package com.hs.controller;


import com.hs.entity.Grade;
import com.hs.entity.Result;
import com.hs.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/tea")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    //跳转到成绩列表
    @RequestMapping("/score")
    public String gradeList(){
        return "teacher/grade-list";
    }
    //获取超级列表信息
    @RequestMapping("/list/grade")
    @ResponseBody
    public Map gradeListJson(String searchKey){
        List<Grade> list=gradeService.findGradeList("%"+searchKey+"%");
        Map<String,Object> map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.size());
        map.put("data",list);
        return map;
    }
    //跳转到成绩录入界面
    @RequestMapping("/score/insert")
    public String gradeInsert(int id, Model model ){
        model.addAttribute("id",id);
        model.addAttribute("usualscore",null);
        model.addAttribute("testscore",null);
        return "teacher/grade-insert";
    }
    //保存成绩信息
    @RequestMapping("/setGrade")
    @ResponseBody
    public Result setGrade(Integer id, String usualscore,String testscore){
        Integer row=gradeService.setGrade(id,usualscore,testscore);
        Result result=new Result();
        if(row>0){
            result.setCode(0);
            result.setMsg("添加成功");
        }else {
            result.setCode(10);
            result.setMsg("添加失败");
        }

        return result;
    }
    //跳转到成绩编辑页面
    @RequestMapping("/score/editgrade")
    public String editgrade(int id, Model model ){
        model.addAttribute("id",id);
        Grade grade = gradeService.findGradeById(id);
        model.addAttribute("gradeInfo",grade);
        return "teacher/grade-edit";
    }



}
